package com.sw.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sw.BaseContext;
import com.sw.constant.LoginConstant;
import com.sw.dto.UserLoginDTO;
import com.sw.entity.impl.SysDict;
import com.sw.entity.impl.SysMenu;
import com.sw.entity.impl.SysUser;
import com.sw.exception.BusinessException;
import com.sw.mapper.SysDictMapper;
import com.sw.mapper.SysMenuMapper;
import com.sw.mapper.SysRoleMapper;
import com.sw.mapper.SysUserMapper;
import com.sw.service.IndexService;
import com.sw.utils.IpUtils;
import com.sw.utils.JwtUtil;
import com.sw.vo.LoginVO;
import com.sw.vo.MenuVO;
import com.sw.vo.ProfileVO;
import com.sw.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 基础设置操作类
 *
 * @project: com.sw.service.impl
 * @author: Szh
 * @date: 2023/9/19 19:06
 */
@Service
@RequiredArgsConstructor
public class IndexServiceImpl implements IndexService {
    private final JwtUtil jwtUtil;
    private final IpUtils ipUtils;

    private final SysUserMapper sysUserMapper;
    private final SysRoleMapper sysRoleMapper;
    private final SysMenuMapper sysMenuMapper;
    private final SysDictMapper sysDictMapper;

    @Override
    public LoginVO login(UserLoginDTO userLoginDTO) {
        SysUser sysUser = sysUserMapper.selectByLoginName(userLoginDTO.getLogin_name());
        if (Objects.isNull(sysUser)) {
            throw new BusinessException("账号不正确");
        }
        String digest = DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        if (!Objects.equals(digest, sysUser.getPassword())) {
            throw new BusinessException("密码不正确");
        }

        sysUser.setLoginIp(ipUtils.getClientIp());
        sysUser.setLoginDate(LocalDateTime.now());
        sysUserMapper.updateById(sysUser);

        HashMap<String, Object> map = new HashMap<>(8);
        map.put(LoginConstant.USER_ID, sysUser.getId());
        String jwt = jwtUtil.createJWT(map);
        return LoginVO.builder().token(jwt).build();
    }

    @Override
    public ProfileVO profile() {
        ProfileVO profileVO = new ProfileVO();
        UserInfoVO userInfoVO = getUserInfoVO();
        List<MenuVO> menuVOS = getMenuVOList(userInfoVO);
        Map<String, Object> dictListMap = getDictListMap();
        ArrayList<String> btnList = getBtnList();

        profileVO.setUserInfo(userInfoVO);
        profileVO.setMenuList(menuVOS);
        profileVO.setDictsList(dictListMap);
        profileVO.setBtnList(btnList);

        return profileVO;
    }

    private ArrayList<String> getBtnList() {
        ArrayList<String> btnList = new ArrayList<>();
        btnList.add("*");
        return btnList;
    }
    private Map<String, Object> getDictListMap() {
        HashMap<String, Object> result = new HashMap<>(16);
        List<SysDict> sysDictList = sysDictMapper.selectList();
        sysDictList.forEach(sysDict -> {
            HashMap<String, Object> item = new HashMap<>(8);
            item.put(sysDict.getValue(), sysDict.getLabel());
            result.put(sysDict.getType(), item);
        });
        return result;
    }
    private List<MenuVO> getMenuVOList(UserInfoVO userInfoVO) {
        List<SysMenu> sysMenus = sysMenuMapper.selectListByRoleIds(userInfoVO.getRole_id());
        return sysMenus.stream().map(SysMenu::conversion).collect(Collectors.toList());
    }
    private UserInfoVO getUserInfoVO() {
        SysUser sysUser = sysUserMapper.selectById(BaseContext.getLoginUserId());
        String roleIdJsonArr = sysUser.getRoleId();

        UserInfoVO userInfoVO = sysUser.conversion();

        if (StringUtils.isNotBlank(roleIdJsonArr)) {
            JSONArray roleIds = JSON.parseArray(roleIdJsonArr);
            List<String> roleIdsJavaList = roleIds.toJavaList(String.class);
            userInfoVO.setRole_id(roleIdsJavaList);
            List<String> roleNameList = sysRoleMapper.selectNameListByIds(roleIdsJavaList);
            userInfoVO.setRolename(roleNameList);
        }
        return userInfoVO;
    }
}
